package com.sky.controller.admin;

import java.util.List;

import com.github.pagehelper.Page;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;


@RestController
@RequestMapping("/admin/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;


    /**
     * 后台取消订单
     *
     * @param ordersCancelDTO
     * @return
     */
    @PutMapping("/cancel")
    public Result cancelOrder(@RequestBody OrdersCancelDTO ordersCancelDTO) {
        log.info("取消订单原因:{}", ordersCancelDTO);
        Orders orders = new Orders();
        orders.setCancelTime(LocalDateTime.now());
        orders.setStatus(Orders.CANCELLED);
        BeanUtils.copyProperties(ordersCancelDTO, orders);
        orderService.updateOrder(orders);
        return Result.success();
    }

    /**
     * 获取订单状态
     *
     * @return
     */
    @GetMapping("/statistics")
    public Result<OrderStatisticsVO> statistics() {
        log.info("获取订单状态");
        OrderStatisticsVO orderStatistics = orderService.getOrderStatistics();
        return Result.success(orderStatistics);
    }

    @PutMapping("/complete/{id}")
    public Result completeOrder(@PathVariable Long id) {
        log.info("订单完成");
        Orders orders = Orders.builder().id(id).build();
        orders.setStatus(Orders.COMPLETED);
        orderService.updateOrder(orders);
        return Result.success();
    }

    /**
     * 商户拒绝接单
     *
     * @param ordersRejectionDTO
     * @return
     */
    @PutMapping("rejection")
    public Result rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO) {
        log.info("拒绝订单 原因:{}", ordersRejectionDTO);
        Orders orders = Orders.builder().id(ordersRejectionDTO.getId()).build();
        orders.setRejectionReason(ordersRejectionDTO.getRejectionReason());
        orders.setCancelTime(LocalDateTime.now());
        orders.setStatus(Orders.CANCELLED);
        orderService.updateOrder(orders);
        return Result.success();
    }

    /**
     * 接单
     *
     * @param ordersConfirmDTO
     * @return
     */
    @PutMapping("/confirm")
    public Result confirm(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
        log.info("接受订单:{}", ordersConfirmDTO);
        Orders orders = Orders.builder().id(ordersConfirmDTO.getId()).build();
        orders.setStatus(Orders.CONFIRMED);
        orderService.updateOrder(orders);
        return Result.success();
    }

    /**
     * 派送订单
     *
     * @param id
     * @return
     */
    @PutMapping("/delivery/{id}")
    public Result delivery(@PathVariable Long id) {
        log.info("订单派送");
        Orders orders = Orders.builder().id(id).build();
        orders.setStatus(Orders.DELIVERY_IN_PROGRESS);
        orderService.updateOrder(orders);
        return Result.success();
    }

    /**
     * 订单详情
     *
     * @param id
     * @return
     */
    @GetMapping("/details/{id}")
    public Result<OrderVO> details(@PathVariable Long id) {
        log.info("订单详情");
        OrderVO vo = orderService.detailOrder(id);
        return Result.success(vo);
    }

    @GetMapping("/conditionSearch")
    public Result<PageResult> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("查询条件:{}", ordersPageQueryDTO);
        List<OrderVO> orderDetailAll = orderService.getOrderDetailAll(ordersPageQueryDTO);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(orderDetailAll.size());
        pageResult.setRecords(orderDetailAll);
        return Result.success(pageResult);
    }

}
